package com.yanqu.road.server.cmd.union.activitygroup;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;

@Cmd(code = CrossProtocol.C2_UNION_ACTIVITY_GROUP_GET_MEMBERS, desc = "获取本商会的参赛名单")
public class CrossUnionActivityGroupGetMemberCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionActivityGroupProto.UnionActivityGroupGetMemberReqMsg reqMsg = UnionActivityGroupProto.UnionActivityGroupGetMemberReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();

        UnionActivityGroupUnion union = Cross2UnionActivityGroupMgr.getUnionData(activityId, packet.getPlayerId());
        if(union != null){
            UnionActivityGroupProto.UnionActivityGroupGetMemberRespMsg.Builder clientMsg = UnionActivityGroupProto.UnionActivityGroupGetMemberRespMsg.newBuilder();
            clientMsg.setActivityId(activityId);
            clientMsg.setRet(0);
            if(union.getLocked() == 1){
                for (Map.Entry<Long, UnionActivityGroupUser> userEntry : union.getUserMap().entrySet()) {
                    UnionActivityGroupUser groupUser = userEntry.getValue();
                    UnionActivityGroupProto.UnionActivityGroupUserDetailMsg.Builder builder = UnionActivityGroupProto.UnionActivityGroupUserDetailMsg.newBuilder();
                    builder.setUserId(groupUser.getUserId());
                    builder.setPosition(groupUser.getPosition());
                    builder.setServerId(groupUser.getServerId());
                    UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(groupUser.getUserId());
                    if(userBaseInfo != null) {
                        builder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                        clientMsg.addUser(builder);
                    }else {
                        UnionActivityGroupProto.CrossGetUserBaseInfoReqMsg.Builder toGame = UnionActivityGroupProto.CrossGetUserBaseInfoReqMsg.newBuilder();
                        toGame.addUserId(groupUser.getUserId());
                        toGame.setActivityId(activityId);
                        toGame.setGroupId(union.getGroupId());
                        MessageHelper.sendPacket(groupUser.getServerId(), 0, YanQuMessageUtils.buildMessage(Protocol.S_UNION_ACTIVITY_GROUP_GET_USER_INFO_FROM_CROSS, toGame));
                    }
                }
            }
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_ACTIVITY_GROUP_GET_MEMBERS, clientMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
